<?php
include('../../includes/classes/core.php');
$start  	= $_REQUEST['start'];
$end    	= $_REQUEST['end'];
$count 		= $_REQUEST["count"];
$action 	= $_REQUEST['act'];
$departament= $_REQUEST['departament'];
$type       = $_REQUEST['type'];
$category   = $_REQUEST['category'];
$s_category = $_REQUEST['sub_category'];
$done 		= $_REQUEST['done']%2;
$name 		= $_REQUEST['name'];
$title 		= $_REQUEST['title'];
$text[0] 	= "შემოსული  ზარები ობიექტების  მიხედვით";
$text[1] 	= "'$departament'- შემოსული ზარები  კატეგორიების მიხედვით";
$text[2] 	= "'$departament'- შემოსული ზარები ქვე-კატეგორიების  მიხედვით";
$text[3] 	= "'$departament'- შემოსული  ქვე–კატეგორიის მიხედვით";
$c="3 or incomming_call.call_type_id=0";
if ($type=="ინფორმაცია")  $c=1;
elseif ($type=="პრეტენზია") $c=2;
elseif ($type=="სხვა") $c=3;

$start = $db->convertDateTime($start);
$end = $db->convertDateTime($end);
//------------------------------------------------query-------------------------------------------
switch ($done){
	case  1:
		$result = mysql_query("SELECT call_type.`name` AS type,
										COUNT(*),
										CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM incomming_call
										JOIN object ON incomming_call.object_id=object.id
										JOIN call_type ON call_type.`id`=incomming_call.call_type_id
											WHERE object.`name`='$departament' and `incomming_call`.`date` >= '$start' AND `incomming_call`.`date` <= '$end')*100,2),'%')
								FROM 	incomming_call
									JOIN object ON incomming_call.object_id=object.id
									JOIN call_type ON call_type.`id`=incomming_call.call_type_id
								WHERE object.`name`='$departament' and	`incomming_call`.`date` >= '$start' and  `incomming_call`.`date` <= '$end'
								GROUP BY 	type");
		$text[0]=$text[1];
	break;
	default:
		$result = mysql_query("SELECT 	object.`name` AS d_name,
										COUNT(*),
										CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM incomming_call JOIN object ON incomming_call.object_id=object.id
										WHERE `incomming_call`.`date` >= '$start' AND `incomming_call`.`date` <= '$end')*100,2),'%')
							FROM 		incomming_call
							JOIN 		object ON incomming_call.object_id=object.id
							WHERE 		`incomming_call`.`date` >= '$start' AND `incomming_call`.`date` <= '$end'
							GROUP BY 	d_name");

		break;
}
///----------------------------------------------act------------------------------------------
switch ($action) {
	case "get_list":
		$data = array("aaData"	=> array());
		while ( $aRow = mysql_fetch_array( $result ) )
		{	$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[0] = '0';

				$row[$i+1] = $aRow[$i];
			}
			$data['aaData'][] =$row;
		}
		echo json_encode($data); return 0;
		break;
	case 'get_category' :
		$rows = array();
		while($r = mysql_fetch_array($result)) {
			$row[0] = $r[0];
			$row[1] = (float) $r[1];
			$rows['data'][]=$row;
		}
		$rows['text']=$text[0];
		echo json_encode($rows);
		break;
		case 'get_in_page':
			mysql_query("SET @i = 0;");
			$rResult = mysql_query("SELECT  		inc.id,    
                        						inc.id, 
                        						DATE_FORMAT(inc.`date`,'%d/%m/%y %H:%i:%s') AS `date`,
                        						inc.pin,
												inc.pid,
                        						inc.phone,
                        						category.`name`,
                        						`crystal_users`.`name` ,
                        						`crystal_users1`.`name` ,
                        						if(ISNULL(task.incomming_call_id), 'დასრულებული', `status`.`call_status`)AS `status`,	
                      							TIME_FORMAT(SEC_TO_TIME(asterisk_incomming.duration - 1), '%i:%s') AS `duration`,
												CONCAT('<button class=\'download\' str=',asterisk_incomming.file_name,'>მოსმენა</button>')AS `file_name`
									FROM 				incomming_call AS inc
	                                LEFT JOIN 	category  ON inc.call_category_id=category.id
	                                LEFT JOIN 	task ON task.incomming_call_id=inc.id								
	                                LEFT JOIN 	crystal_users ON task.responsible_user_id=crystal_users.id	
	                                LEFT JOIN 	crystal_users   AS `crystal_users1`	ON inc.user_id=crystal_users1.id
	                                LEFT JOIN 	`status` ON task.`status`=`status`.id
									LEFT JOIN 	asterisk_incomming ON inc.uniqueid=asterisk_incomming.uniqueid
									LEFT JOIN   call_type ON inc.call_type_id=call_type.id
									LEFT JOIN 	object ON inc.object_id=object.id
									WHERE 
									`inc`.`date` >= '$start' AND `inc`.`date` <= '$end'
									AND
									call_type.`name`='$_REQUEST[rid]'	AND object.`name`='$type'					
									GROUP BY inc.id");
					$data = array(
							"aaData"	=> array()
					);
		
					while ( $aRow = mysql_fetch_array( $rResult ) )
					{
					$row = array();
					$row1 = array();
		
					for ( $i = 0 ; $i < $count ; $i++ )
					{
					$row[] = $aRow[$i];
					$a=$aRow;
		
					{
		
					}
					}
						$data['aaData'][] = $row;
					}
					echo json_encode($data); return 0;
					break;
	default :
		echo "Action Is Null!";
		break;

}



?>